import axios from 'axios';
import { fromJS } from 'immutable';
import * as actionTypes from './actionTypes'

const homeAction = (result) => ({
    type: actionTypes.GET_HOME_DATA,
    topicList: result.topicList,
    articleList: result.articleList,
    recommendList: result.recommendList
})

const getListData = (list, page) => ({
    type: actionTypes.GET_ARTICLE_DATA,
    list: fromJS(list),
    page
})

export const getHomeData = () => {
    return (dispatch) => {
        axios.get('/api/home.json').then(res => {
            const result = res.data.data
            dispatch(homeAction(result))
        })
    }
}
// 获取更多新闻列表
export const getMoreList = (page) => {
    return (dispatch) => {
        axios.get('/api/homeList.json?page=' + page).then(res => {
            const list = res.data.data
            dispatch(getListData(list, page + 1))
        })
    }
}

export const changeShowScrollTop = (show) => ({
    type: actionTypes.TOGGLE_SCROLLTOP_SHOW,
    show
})